package org.attached.user.config.mybatis_plus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * mybatis-plus代码生成器
 * 
 * @author Mr.古
 *
 */
public class MyBatisPlusGenerator {
	public final static String DRIVER_NAME = "com.mysql.jdbc.Driver";
	public final static String USERNAME = "root";
	public final static String PASSWORD = "";
	public final static String JDBC_URL = "jdbc:mysql://localhost:3306/chat_room?useUnicode=true&characterEncoding=UTF8";
	
	/**
	 * 配置全局
	 * 
	 * @author Mr.古
	 * @return
	 */
	public static GlobalConfig getGlobalConfig() {
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir("C:\\eclipse-project\\chatRoom-attached\\attached-user\\src\\main\\java");// 这里写你自己的java目录
		gc.setFileOverride(true);// 是否覆盖
		gc.setActiveRecord(true);
		gc.setEnableCache(false);// XML 二级缓存
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(true);// XML columList
		gc.setAuthor("Mr.古");

		// 自定义文件命名，注意 %s 会自动填充表实体属性！
		gc.setMapperName("%sMapper");
		gc.setXmlName("%sMapper");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setControllerName("%sController");
		return gc;
	}

	/**
	 * 数据源配置
	 * 
	 * @author Mr.古
	 * @return
	 */
	public static DataSourceConfig getDataSourceConfig() {
		// 数据源配置processTypeConvert
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		// 数据库类型
		dataSourceConfig.setDbType(DbType.MYSQL);
		dataSourceConfig.setTypeConvert(new MySqlTypeConvert() {
			// 自定义数据库表字段类型转换【可选】
			@Override
			public DbColumnType processTypeConvert(String fieldType) {
				return super.processTypeConvert(fieldType);
			}
		});
		dataSourceConfig.setDriverName(DRIVER_NAME);
		dataSourceConfig.setUsername(USERNAME);
		dataSourceConfig.setPassword(PASSWORD);
		dataSourceConfig.setUrl(JDBC_URL);
		return dataSourceConfig;
	}

	/**
	 * 策略配置
	 * 
	 * @author Mr.古
	 * @return
	 */
	public static StrategyConfig getStrategyConfig() {
		StrategyConfig strategy = new StrategyConfig();
		// 此处可以修改为您的表前缀
		// strategy.setTablePrefix(new String[]{"pf_"});
		// 表名生成策略
		strategy.setNaming(NamingStrategy.underline_to_camel);
		// 需要生成的表
		 strategy.setInclude(new String[] { "chat_user" });
		// 排除生成的表
		strategy.setExclude();

		// 自定义实体父类
		// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
		// 自定义实体，公共字段
		// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
		// 自定义 mapper 父类
		// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
		// 自定义 service 父类
		// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
		// 自定义 service 实现类父类
		// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
		// 自定义 controller 父类
		// strategy.setSuperControllerClass("com.github.wxiaoqi.security.business.controller.base.BaseShopController");
		// 生成 RestController 风格
		strategy.setRestControllerStyle(true);
		return strategy;
	}

	/**
	 * 包配置
	 * 
	 * @author Mr.古
	 * @return
	 */
	public static PackageConfig getPackageConfig() {
		// 注意不同的模块生成时要修改对应模块包名
		PackageConfig packageConfig = new PackageConfig();
		packageConfig.setParent(null);
		packageConfig.setEntity("org.attached.user.entity");
		packageConfig.setMapper("org.attached.user.mapper");
//		packageConfig.setXml(null);
		packageConfig.setService("org.attached.user.service");
		packageConfig.setServiceImpl("org.attached.user.service.impl");
		packageConfig.setController("org.attached.user.controller");
		return packageConfig;
	}

	/**
	 * 配置Maper配置文件
	 * 
	 * @author Mr.古
	 * @return
	 */
	public static InjectionConfig getInjectionConfig() {
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				this.setMap(map);
			}
		};
		// 调整 xml 生成目录演示
		List<FileOutConfig> focList = new ArrayList<>();
		focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return "C:\\eclipse-project\\chatRoom-attached\\attached-user\\src\\main\\resources\\mapper\\" + tableInfo.getEntityName() + "Mapper.xml";
			}
		});
		cfg.setFileOutConfigList(focList);
		return cfg;
	}

	public static void main(String[] args) {
		AutoGenerator autoGenerator = new AutoGenerator();
		// 全局配置
		autoGenerator.setGlobalConfig(getGlobalConfig());
		// 数据源配置
		autoGenerator.setDataSource(getDataSourceConfig());
		// 策略配置
		autoGenerator.setStrategy(getStrategyConfig());
		// 包配置
		autoGenerator.setPackageInfo(getPackageConfig());
		// mapper文件配置
		autoGenerator.setCfg(getInjectionConfig());
		// 配置生成模板
		TemplateConfig tc = new TemplateConfig();
		tc.setXml(null);
		autoGenerator.setTemplate(tc);
		// 执行生成
		autoGenerator.execute();
	}
}
